package com.seckill.act.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seckill.act.mapper.SeckillTimeMapper;
import com.seckill.act.pojo.SeckillTime;
import com.seckill.act.service.SeckillTimeService;
import com.seckill.framework.mybatisplus.WrapperParse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2022-03-04
 */
@Service
public class SeckillTimeServiceImpl extends ServiceImpl<SeckillTimeMapper, SeckillTime> implements SeckillTimeService {

    @Autowired
    private SeckillTimeMapper seckillTimeMapper;

    /***
     * 查询当前时段是否已存在
     * @param starttime
     * @param endtime
     * @return
     */
    @Override
    public Long findByTimesCount(String starttime, String endtime) {
        QueryWrapper<SeckillTime> wrapper = WrapperParse.simpleColumns("starttime,endtime", starttime, endtime);
        return seckillTimeMapper.selectCount(wrapper);
    }

    /**
     * SeckillTime条件+分页查询
     * @param seckillTime 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<SeckillTime> findPage(SeckillTime seckillTime, int page, int size){
        //分页
        IPage<SeckillTime> iPage = new Page<SeckillTime>(page,size);
        //搜索条件构建
        QueryWrapper<SeckillTime> wrapper = WrapperParse.simple(seckillTime);
        //执行搜索
        return (Page<SeckillTime>) seckillTimeMapper.selectPage(iPage,wrapper);
    }

    /**
     * SeckillTime分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<SeckillTime> findPage(int page, int size){
        //静态分页
        IPage<SeckillTime> iPage = new Page<SeckillTime>(page,size);
        //分页查询
        return (Page<SeckillTime>) seckillTimeMapper.selectPage(iPage,null);
    }

    /**
     * SeckillTime条件查询
     * @param seckillTime
     * @return
     */
    @Override
    public List<SeckillTime> findList(SeckillTime seckillTime){
        //构建查询条件
        QueryWrapper<SeckillTime> wrapper = WrapperParse.simple(seckillTime);
        //根据构建的条件查询数据
        return seckillTimeMapper.selectList(wrapper);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        seckillTimeMapper.deleteById(id);
    }

    /**
     * 修改SeckillTime
     * @param seckillTime
     */
    @Override
    public void update(SeckillTime seckillTime){
        seckillTimeMapper.updateById(seckillTime);
    }

    /**
     * 增加SeckillTime
     * @param seckillTime
     */
    @Override
    public void add(SeckillTime seckillTime){
        seckillTimeMapper.insert(seckillTime);
    }

    /**
     * 根据ID查询SeckillTime
     * @param id
     * @return
     */
    @Override
    public SeckillTime findById(Integer id){
        return  seckillTimeMapper.selectById(id);
    }

    /**
     * 查询SeckillTime全部数据
     * @return
     */
    @Override
    public List<SeckillTime> findAll(String name) {
        if(!ObjectUtils.isEmpty(name)){
            QueryWrapper<SeckillTime> wrapper = WrapperParse.simpleColumnLike("name", name);
            return seckillTimeMapper.selectList(wrapper);
        }
        return seckillTimeMapper.selectList(null);
    }

    /***
     * 审核
     * @param id
     * @param type
     */
    @Override
    public void audit(Integer id, Integer type) {
        SeckillTime seckillTime = new SeckillTime();
        seckillTime.setStatus(type);
        seckillTime.setId(id);
        seckillTimeMapper.updateById(seckillTime);
    }

    /***
     * 查询所有有效时间
     * @return
     */
    @Override
    public List<SeckillTime> findAllValidTimes() {
        //已启用
        QueryWrapper<SeckillTime> wrapper = WrapperParse.simpleColumn("status",1);
        return seckillTimeMapper.selectList(wrapper);
    }

    /***
     * 删除所有时间段
     */
    @Override
    public void deleteAll() {
        seckillTimeMapper.delete(null);
    }

    /***
     * 批量增加时间段
     * @param seckillTimes
     */
    @Override
    public void addTimes(List<SeckillTime> seckillTimes) {
        for (SeckillTime seckillTime : seckillTimes) {
            seckillTimeMapper.insert(seckillTime);
        }
    }


    /***
     * 转换
     * @throws ParseException
     */
    public Date parse(Date date)  {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            String format = sdf.format(date);
            return sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
